<?php
defined('BASEPATH') or exit ('No direct script access allowed');

class Role extends MY_New_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->datas['page_title'] = "角色管理列表";
    }

    public function index()
    {
        check_privilege(210);   //权限检测
        $this->_query_list();
    }

    //跳转到详情页
    public function info($id = '')
    {
        if ($id == '') {
            check_privilege(211);   //权限检测
            $info['id']                = $id;
            $info['active']            = 1;
            $info['privilege_ids']     = [];
            $this->datas['page_title'] = "添加角色管理";
        } else {
            check_privilege(212);   //权限检测
            $info                      = $this->_myModel->get_info($id);
            $info['privilege_ids']     = explode(',', $info['privilege_ids']);
            $this->datas['page_title'] = "详情";
        }
        $this->datas['form_action'] = $id == '' ? 'insert' : 'update';
        $this->datas['info']        = $info;
        $this->load->view($this->_dwt_info, $this->datas);
    }

    /**
     * 处理各种ajax请求
     * @param unknown $action
     * @param unknown $datas
     * @return json
     */
    public function process_ajax($action, $data_results = array())
    {
        $content        = '';
        $message        = '';
        $append_data    = array();
        $exclude_for_id = array('add', 'insert', 'query', 'hide', 'show', 'edit', 'update', 'delete');
        if (in_array($action, array('update', 'do_modify'))) {
            $data_results['item_id'] = $data_results['id'];
        }
        if (!in_array($action, $exclude_for_id)) {
            make_json_error("未指明操作对象！[305:$action]");
        }

        $result['error'] = 0;
        switch ($action) {
            case 'query':
                check_privilege(210);   //权限检测
                $this->datas ['full_page'] = FALSE;
                $content                   = $this->_query_list();
                //统计数量
                $append_data['trash_count'] = $this->_myModel->get_trash_count();
                break;
            case 'hide':
            case 'show':
                check_privilege(212);   //权限检测
                $update_datas['active'] = $action == 'hide' ? 0 : 1;
                $result                 = $this->_myModel->edit_data($update_datas, $data_results['item_id']);
                break;
            case 'insert':
                check_privilege(211);   //权限检测
                $result = $this->_myModel->add_data($data_results);
                if ($result['error'] == 1) {
                    make_json_error($result['message']);
                } else {
                    $message                 = '添加角色成功';
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url($this->datas['my_router']);
                };
                break;
            case 'update':
                check_privilege(212);   //权限检测
                $result = $this->_myModel->edit_data($data_results, $data_results['item_id'], FALSE);
                if ($result['error'] == 1) {
                    make_json_error($result['message']);
                } else {
                    $message                 = '修改角色成功';
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url($this->datas['my_router']);
                }
                break;
            // 逻辑删除
            case 'delete':
                check_privilege(213);   //权限检测
                $update_datas['is_delete'] = 1;
                $result                  = $this->_myModel->logically_delete($data_results['item_id'], $update_datas);
                $append_data['followUp'] = 'queryList';
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $data_results['item_id'] . "]";
                break;
        }
        if ($result['error'] > 0) {
            make_json_error($result['message']);
        } else {
            make_json_result($content, $message, $append_data);
        }
    }


}
